<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of report
 *
 * @author Константин Охотник
 */
class Crud extends CI_Model
{
    public $path;
    public function __construct() 
    {
        parent::__construct();
        $this->path['settings_page'] = "./application/settings_page/";
        $this->path['path_template'] =$this->path['static'] = "application/views/";        
    }
    
//============Возврат количества строк =========================================
    public function row_nums($table, $row = FALSE, $cell = FALSE)
    {      
        if ( gettype($row) == 'array')
        {
            $arr = array_combine($row, $cell); 
        }
        else
        {
            $arr[$row] = $cell;  
        }
        if($row!=false && $cell !=false)
        {
            $query = $this->db->get_where($table, $arr);
            return $query->num_rows();
        }
        else
         {
            $query = $this->db->get($table);
            return $query->num_rows();
         }
    }
 
//============Возврат массива данных============================================    
    public function get_array($table, $row = FALSE, $cell = FALSE, $num =FALSE, $offset = FALSE)
    {
        if ( gettype($row) == 'array')
            $arr = array_combine($row, $cell); 
        
        if($row !=false && gettype($row) != 'array'||'boolean')
            $arr[$row] = $cell;
            
       if($row!=false && $cell !=false)
            $query = $this->db->get_where($table,$arr );
        
         if($num != false && $offset != false)
            $query = $this->db->get($table, $num, $offset);
         
         else 
            $query = $this->db->get($table);
         
         return $query->result_array();
    }
    
//============Возврат  один элемент из базы=====================================    
    public function get_one($table, $row, $cell)
    {
        $query = $this->db->get_where($table, array($row => $cell));
        return  $query->result_array();
    }
    
//=============================Обновление данных================================
    public function update_post($table, $row, $cell, $data)
    {
        $query = $this->db->where($row, $cell);
        $update = $this->db->update($table, $data); 
        return $update;
    } 
   
//===========================Удаление данных====================================
     public function delete_post($table, $row, $cell)
    {
        $query = $this->db->where($row, $cell);
        $delete = $this->db->delete($table);  
        return $delete;
    }
    
//==========================Добавление данных===================================
    public function add_db($table, $data)
    {
       $add =  $this->db->insert($table, $data); 
       return $add;
    }
    
//=======================Распаковка архива======================================
    public function unzip($full_name_zip )
    {
        $pclzip_config['p_zipname'] = $full_name_zip; 
        $this->pclzip->unzip($pclzip_config); 
        // Распаковываем архив 
        $result = $this->pclzip->extract('./temp/'); 
        // И тут же его удаляем 
        unlink($full_name_zip); 
        // Получаем список фотографий 
        return $result;
    }

//=======================Вставка перпеменных в шаблон=========================== 
   public function assign($name ,$array_list)
   {
        $this->ci_smarty->assign($name,  $array_list);
        return $this;
   }
    
//=======================Вставка перпеменных в шаблон===========================
   public function  display($tpl)
   {
        $this->ci_smarty->display($tpl);
   }
   
//======================================Сравнение дат===========================  
   public function actualitydate($date_start, $date_end)
   {
        if($date_start <= $date_end)
        {
            return 1;
        }
        
        if ($date_start > $date_end)
        {
            return 0;
        }
   }
   
//======================возвращает массив продуктов============================= 
   public function parse_product($str)
   {
        return explode(',',$str);
   }
   
//======================возвращает массив продуктов=============================    
   protected function _getProduct(&$key, &$r,$i )
    {
       $key['id_product'] = $this->parse_product($key['id_product']);
       $r[$i]['count']    = $this->parse_product($r[$i]['count']);
       foreach($key['id_product'] as $k => $value)
       {
           $p = $this->get_one('tb_product', 'id', $value);
           $r[$i]['product'][] = $p[0]['name'];
           $r[$i]['price'][] = $p[0]['price'];
           $p = $this->get_one('tb_fabricator', 'id', $p[0]['id_fabricator']);
           $r[$i]['fabricator'][] =  $p[0]['name'];
       }
    }
}

